//
//  XMLHomePicView.swift
//  Weibo
//
//  Created by mac on 16/5/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

let itemMargin :CGFloat = 5
let itemWidth = (screenWidth - 2*margin - 2*itemMargin) / 3
private let reuseID = "pic"

class XMLHomePicView: UICollectionView,UICollectionViewDataSource{
    
    
    var picUrls :[XMLHomePic]? {
    
        didSet {
        
            countLable.text = "\(picUrls?.count ?? 0)"
            let currentSize = calculateSize(picUrls?.count ?? 0)
//            print(picUrls)
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(currentSize)
            }
            reloadData()
            
        }
    }
    
    private lazy var countLable :UILabel = UILabel(textColor: UIColor.blueColor(), fontSize: 15)
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
//        print(CGSize(width: itemWidth, height: itemWidth))
        flowLayout.minimumInteritemSpacing = itemMargin
        flowLayout.minimumLineSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    private func setUpUI() {
    
        addSubview(countLable)
        
        countLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        registerClass(XMLHomePicCell.self, forCellWithReuseIdentifier: reuseID)
         
        dataSource = self
            
        }
    }
    private func calculateSize(count :Int)->CGSize {
    
        let cols = count > 3 ? 3 : count
        
        let rows = (count - 1) / 3 + 1
        
        let currentWidth = CGFloat(cols)*itemWidth + CGFloat(cols - 1)*itemMargin
        let currentHeight = CGFloat(rows)*itemWidth + CGFloat(rows - 1)*itemMargin
        return CGSize(width: currentWidth, height: currentHeight)
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! XMLHomePicCell
        cell.homePic = picUrls![indexPath.row]
        
        return cell
    }

}




class XMLHomePicCell :UICollectionViewCell{
    
    var homePic :XMLHomePic? {
    
        didSet {
        
            guard let url = homePic?.thumbnail_pic else {
                return
            }
            
            picImageView .sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            
            gifImageView.hidden = !url.hasSuffix(".gif")
            
        }
    }
    
    private lazy var picImageView :UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
        
    }()
    private lazy var gifImageView :UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
        return imageView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        addSubview(picImageView)
        addSubview(gifImageView)
        
        picImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(picImageView)
            make.bottom.equalTo(picImageView)
        }
        
    }
    
    
    
}
